//Ext.BLANK_IMAGE_URL = '../../resources/images/default/s.gif';
var timer, state;
var msgDiv, checkboxDelete;

function init(){
    var container = document.getElementById('container');
    msgDiv = document.getElementById('optionsDiv');
    if (!container) {
		return setTimeout(init, 10);
	}
    checkboxDelete = document.getElementById('checkboxDelete');
    if(wave && wave.isInWaveContainer()){
		wave.setStateCallback(stateUpdated);
	}
    gadgets.window.adjustHeight();
    updateCheckState();
}

gadgets.util.registerOnLoadHandler(init);

function stateUpdated(){
    state = wave.getState();
    if(!state || !state.state_){
        return;
    }
    var checked = state.get('checkboxDelete');
    if(checked == null){
        checkboxDelete.checked = false;
    }
    else{
        checkboxDelete.checked = (checked == 'true');
    }
}

function createBox(){
	return ['<div onBlur="hideOptions()" style="position: fixed; z-index: 3250; display: block; opacity: 1; ">',
            '<div style="float:left;height:26px;width:7px;background-image:url();"></div>',
            '<div style="height:26;float:left;background-image:url();">',
                '<div style="font-size: 8pt;padding: 5px 10px 0px 2px;float:left;" title="Insert reply here" onclick="deleteOption()">Delete</div>',
                '<div style="font-size: 8pt;padding: 5px 6px 0px 2px;float:left;" title="Edit this message" onclick="deleteSelectedOption()">Delete Selected</div>',
            '<div style="font-size: 8pt;padding: 5px 6px 0px 2px;float:left;" title="Edit this message" onclick="collapse()">Collapse all</div>',
            '</div>',
            '<div style="height:26;width:9;float:left;background-image:url();"></div>',
            '</div>'].join('');
}
    
function showOptions(){
    if(msgDiv.hasChildNodes()){
        hideOptions();
        return;
    }
	var box = document.createElement('div');
	box.innerHTML += createBox();
	msgDiv.appendChild(box);
    timer = setTimeout('hideOptions()', 3500);
    //gadgets.window.adjustHeight();
}

function hideOptions(){
    if(timer)
        clearTimeout(timer);
    var optionsDiv = document.getElementById('optionsDiv');
    if(optionsDiv.hasChildNodes()){
        while(optionsDiv.childNodes.length > 0){
            optionsDiv.removeChild(optionsDiv.firstChild);
        }
    }
    //gadgets.window.adjustHeight(); 
}

function updateCheckState(){
    state.submitValue('checkboxDelete', checkboxDelete.checked + '');
}

function deleteOption(){
    state.submitValue('deleteOption', 1);
    //hideOptions();
}

function deleteSelectedOption(){
    state.submitValue('deleteSelectedOption', 1);
    //hideOptions();
}

function collapse(){
    hideOptions();
    var evt = document.createEvent("MouseEvents");
    evt.initMouseEvent("click", true, true, window,
        0, 0, 0, 0, 0, false, false, false, false, 0, null);
    var elements = document.getElementsByClassName('button IUB up cat enabled GUB');
    alert(elements.length);
    for(var i = elements.length-1; i >= 0; i--){
        try{
            elements[i].dispatchEvent(evt);
        }
        catch(e){
        }
    }
}